/*
 * Copyright 2012-2016 the original author or authors. Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

package com.amado.springboot.springbootsessionredis.controller;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

	@GetMapping("/add")
	public User add(HttpServletRequest request) {
		User user = new User();
		user.setName("name1k ");
		user.setPhone("18575599963");
		user.setSex((short) 1);
		request.getSession().setAttribute("NAME", user);
		request.getSession().setAttribute("NAME2", "abcd");
		return user;
	}

	@GetMapping("/get")
	public User get(HttpServletRequest request) {
		return (User) (request.getSession().getAttribute("NAME"));
	}

	@GetMapping("/delete")
	public String delete(HttpServletRequest request) {
		request.getSession().removeAttribute("NAME");
		return "success";
	}
}

class User implements Serializable {

	private String name;
	private String phone;
	private Short sex;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Short getSex() {
		return sex;
	}

	public void setSex(Short sex) {
		this.sex = sex;
	}
}
